package com.jmodata.zxu.service.impl.test;

import com.jmodata.zxu.DBRelevant.DBUtil;
import com.jmodata.zxu.DBRelevant.InsertParam;
import com.jmodata.zxu.DBRelevant.QueryParam;
import com.jmodata.zxu.model.PhoneReturnJson;
import com.jmodata.zxu.pojo.user.MapperUser;
import com.jmodata.zxu.service.dao.test.MapperUserDao;
import com.jmodata.zxu.service.test.MapperUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("mapperUserService")
@Transactional(rollbackFor=Exception.class)
public class MapperUserServiceImpl implements MapperUserService {

    private static final Logger logger = Logger.getLogger(MapperUserService.class);

    /**
     *
     */
    @Autowired(required = false)
    private MapperUserDao mapperUserDao;


    @Override
    public PhoneReturnJson insertMapperUser(MapperUser mp) {
        InsertParam ip = DBUtil.toInsertParam(mp);
        int i = mapperUserDao.add(ip);
        if (i > 0){
            String id = ip.getId();//新增实体类返回ID
            logger.info(id);
            return new PhoneReturnJson(true, "添加成功", null);
        } else{
            return new PhoneReturnJson(false, "添加失败", null);
        }
    }

    @Override
    public PhoneReturnJson updateMapperUser(MapperUser mp) {
        int i = mapperUserDao.update(DBUtil.toUpdateParam(mp, "id"));
        if (i > 0){
            return new PhoneReturnJson(true, "修改成功", null);
        } else{
            return new PhoneReturnJson(false, "修改失败", null);
        }
    }

    @Override
    public PhoneReturnJson delMapperUser(int id) {
        //可删除多个
        int i = mapperUserDao.delete(new Object[] { id });
        if (i > 0){
            return new PhoneReturnJson(true, "删除成功", null);
        } else{
            return new PhoneReturnJson(false, "删除失败", null);
        }
    }

    @Override
    public PhoneReturnJson mapperUserAll(QueryParam param) {
        param.setQueryall(true);//设置查询所有列表
        List<MapperUser> list = mapperUserDao.list(param);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return new PhoneReturnJson(true, "获取成功", map);
    }

    @Override
    public PhoneReturnJson mapperUserList(QueryParam param) {
        List<MapperUser> list = mapperUserDao.list(param);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return new PhoneReturnJson(true, "获取成功", map);
    }


}
